package ui

import (
	"catlib/lib/ui/collision"
	"catlib/lib/ui/draw"
	"catlib/lib/ui/porterduff"
	"image"
	"image/png"
	"os"
)

type object struct {
	APIBaseStruct                     // 依赖基础结构体
	APIUpdateStruct                   // 框架的更新结构体
	Rectangle       draw.APIRectangle // 绘图矩阵
	Base            ObjectBase        // 绘图底层结构体
	StateHide       bool              // 隐藏组件
}

func (obj *object) New(x, y, z, width, height int, Blend porterduff.ColorBlend) {
	obj.Rectangle.TopLeft.X = x
	obj.Rectangle.TopLeft.Y = y
	obj.Rectangle.LowerRight.X = width
	obj.Rectangle.LowerRight.Y = height
	obj.Base = NewObjectBase(width, height, Blend)
}
func (obj *object) GetBase() ObjectBase {
	return obj.Base
}
func (obj *object) GetRectangle() draw.APIRectangle {
	return obj.Rectangle
}
func (obj *object) DrawObject(v Object) {
	topLeft := v.GetRectangle().TopLeft
	obj.Base.DrawBase(topLeft.X, topLeft.Y, v.GetBase())
}
func (obj *object) SetHideDisplay(state bool) {
	obj.StateHide = state
}
func (obj *object) CollisionSpot(x, y int) bool {
	return collision.RectangleToSpot(
		float64(x), float64(y),
		float64(obj.Rectangle.TopLeft.X),
		float64(obj.Rectangle.TopLeft.Y),
		float64(obj.Rectangle.LowerRight.X),
		float64(obj.Rectangle.LowerRight.Y))
}
func (obj *object) CollisionObject(v Object) bool {
	rectangle := v.GetRectangle()
	return collision.RectangleToRectangle(
		float64(obj.Rectangle.TopLeft.X),
		float64(obj.Rectangle.TopLeft.Y),
		float64(obj.Rectangle.LowerRight.X),
		float64(obj.Rectangle.LowerRight.Y),
		float64(rectangle.TopLeft.X),
		float64(rectangle.TopLeft.Y),
		float64(rectangle.LowerRight.X),
		float64(rectangle.LowerRight.Y),
	)
}

type objectBase struct {
	APIBaseStruct
	Color
}

func (base *objectBase) GetColor() Color {
	return base
}
func (base *objectBase) DrawBase(x, y int, obj ObjectBase) {
	base.DrawColor(x, y, obj.GetColor(), false)
}

// NewObject 创建组件
func NewObject() Object {
	return &object{}
}

// NewObjectValue 从值创建
func NewObjectValue(x, y, w, h int, base ObjectBase) Object {
	return &object{
		Rectangle: draw.APIRectangle{
			TopLeft:    draw.APISpot{X: x, Y: y},
			LowerRight: draw.APISpot{X: w, Y: h},
		},
		Base: base,
	}
}

// NewObjectBase 创建组件底层
func NewObjectBase(width, height int, Blend porterduff.ColorBlend) ObjectBase {
	base := &objectBase{}
	base.Color = NewColor()
	base.New(width, height, Blend)
	return base
}

// NewImageObjectBase 从图像创建
// img   要绘制的图像
// Blend 底层绘图模式
func NewImageObjectBase(img image.Image, Blend porterduff.ColorBlend) ObjectBase {
	max := img.Bounds().Max
	base := NewObjectBase(max.X, max.Y, Blend)
	// 拷贝像素
	color := base.GetColor()
	for dx := 0; dx < max.X; dx++ {
		for dy := 0; dy < max.Y; dy++ {
			color.DrawPixel(dx, dy, img.At(dx, dy))
		}
	}
	return base
}

// NewLoadImageObjectBase 从图像加载创建
func NewLoadImageObjectBase(path string, Blend porterduff.ColorBlend) (ObjectBase, error) {
	file, err := os.Open(path)
	if err != nil {
		return nil, err
	}
	defer file.Close()
	img, err := png.Decode(file)
	if err != nil {
		return nil, err
	}
	return NewImageObjectBase(img, Blend), nil
}
